// 实现这个项目的构建任务
const { src, dest, parallel, series, watch, task } = require('gulp')

//删除文件
const del = require('del')

const browserSync = require('browser-sync')
//自动加载插件
const loadPlugins = require('gulp-load-plugins')

const eslint = require('gulp-eslint')

const plugins = loadPlugins()
const bs = browserSync.create()
const data = {
    menus: [
        {
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [
                {
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

task('clean', () => {
    return del(['dist', 'temp'])
})

task('style', () => {
    return src('src/assets/styles/*.scss', { base: 'src' })
        .pipe(plugins.sass({ outputStyle: 'expanded' }))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
})


task('script', () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
})

task('page', () => {
    return src('src/*.html', { base: 'src' })
        .pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新
        .pipe(dest('temp'))
        .pipe(bs.reload({ stream: true }))
})

task('lint', ()=>{
    return src('src/assets/scripts/*.js')
        .pipe(eslint())
        .pipe(eslint.format())
        .pipe(eslint.failAfterError())
})

task('image', () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
})

task('font', () => {
    return src('src/assets/fonts/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
})

task('extra', () => {
    //just copy all public files to dist
    return src('public/**', { base: 'public' })
        .pipe(dest('dist'))
})

task('serve', () => {
    //监控css js 和html的变化进行自动刷新
    watch('src/assets/styles/*.scss', series('style'))
    watch('src/assets/scripts/*.js', series('script'))
    watch('src/*.html', series('page'))
    watch([
        'src/assets/images/**',
        'src/assets/fonts/**',
        'public/**'
    ], bs.reload)

    bs.init({
        notify: false,
        port: 3000,
        server: {
            baseDir: ['temp', 'src', 'public'],
            routes: {
                '/node_modules': 'node_modules'
            }
        }
    })
})

task('useref', () => {
    return src('temp/*.html', { base: 'temp' })
        .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
        //压缩html, js和css
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
            collapseWhitespace: true,
            minifyCSS: true,
            minifyJS: true
        })))
        .pipe(dest('dist'))
})

task('compile', parallel('style', 'script', 'page','lint'))

task('build', series(
    'clean',
    parallel(
        series('compile', 'useref'),
        'image',
        'font',
        'extra'
    )
))

task('start', series('compile', 'serve'))

task('default', series('compile', 'serve'))

//const compile = parallel('style' , script, page)

// 上线之前执行
// const build = series(
//     'clean',
//     parallel(
//         series(compile, useref),
//         image,
//         font,
//         extra
//     )
// )

// const develop = series(compile, serve)

// module.exports = {
//     'clean',
//     build,
//     develop
// }
